package com.kwlt.smc.run;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.PropertyConfigurator;

import com.kwlt.smc.dao.ChannelDao;
import com.kwlt.smc.entity.Channel;
import com.kwlt.smc.entity.ConfigEntity;
import com.kwlt.smc.entity.HttpSmsEntity;
import com.kwlt.smc.http.RunZb000ThreadHttp;
import com.kwlt.smc.http.RunZb001ThreadHttp;
import com.kwlt.smc.http.RunZb003ThreadHttp;
import com.kwlt.smc.http.RunZb100ThreadHttp;
import com.kwlt.smc.http.RunZb101ThreadHttp;
import com.kwlt.smc.http.RunZb200ThreadHttp;
import com.kwlt.smc.http.RunZb201ThreadHttp;
import com.kwlt.smc.util.Constants;


public class HttpRunMain {
	public static void main(String[] args) {
		 PropertyConfigurator.configureAndWatch("config/log4j.properties");
		 File file = null;
		 InputStream in = null;
		 file = new File("config/config.properties");
		 //file = new File("D:\\Workspaces\\http_gw_ld\\config\\config.properties");
		 try {
			 in = new FileInputStream(file);
			 Properties prop = new Properties();
			 prop.load(in);
			 HttpRunMain hrm = new HttpRunMain();
			 hrm.execution(prop);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(in!=null){
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		 
	}
	
	public void execution(Properties prop){
		ConfigEntity config = new ConfigEntity();
		try {
			String numbers = prop.getProperty("NUMBERS");
			if(numbers==null){
				throw new Exception("缺少配置参数[NUMBERS]！");
			}
			config.setNumbers(numbers);
			String userName = prop.getProperty("USERNAME");
			if(userName==null){
				throw new Exception("缺少配置参数[USERNAME]！");
			}
			config.setUserName(userName);
			String password = prop.getProperty("PASSWORD");
			if(password==null){
				throw new Exception("缺少配置参数[PASSWORD]！");
			}
			config.setPassword(password);
			String sendUrl = prop.getProperty("SEND_URL");
			if(sendUrl==null){
				throw new Exception("缺少配置参数[SEND_URL]！");
			}
			config.setSendUrl(sendUrl);
			String rptUrl = prop.getProperty("RPT_URL");
			if(rptUrl==null){
				throw new Exception("缺少配置参数[RPT_URL]！");
			}
			config.setRptUrl(rptUrl);
			String moUrl = prop.getProperty("MO_URL");
			if(moUrl==null){
				throw new Exception("缺少配置参数[MO_URL]！");
			}
			config.setMoUrl(moUrl);
			String isMt = prop.getProperty("IS_MT");
			if(isMt==null){
				throw new Exception("缺少配置参数[IS_MT]！");
			}
			config.setIsMt(isMt);
			String isRpt = prop.getProperty("IS_RPT");
			if(isRpt==null){
				throw new Exception("缺少配置参数[IS_RPT]！");
			}
			config.setIsRpt(isRpt);
			String isMo = prop.getProperty("IS_MO");
			if(isMo==null){
				throw new Exception("缺少配置参数[IS_MO]！");
			}
			config.setIsMo(isMo);
			String operators = prop.getProperty("OPERATORS");
			if(operators==null){
				throw new Exception("缺少配置参数[OPERATORS]！");
			}
			config.setOperators(operators);
			String userid = prop.getProperty("USERID");
			config.setUserid(userid);
			run(config);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void run(ConfigEntity config){
		ChannelDao channelDao = new ChannelDao();
		Channel channel = channelDao.findChannelByNumbers(config.getNumbers());
		if(channel!=null){
			HttpSmsEntity entity = new HttpSmsEntity();
			entity.setAccessNumbers(channel.getAccessNumber());
			entity.setMoUrl(config.getMoUrl());
			entity.setMtPath(channel.getMtDir());
			entity.setOperators(channel.getOperators());
			entity.setUserName(config.getUserName());
			entity.setPassword(config.getPassword());
			entity.setRptPath(channel.getRptDir());
			entity.setRptUrl(config.getRptUrl());
			entity.setSendUrl(config.getSendUrl());
			entity.setTmpPath(channel.getMoDir());
			entity.setNumbers(channel.getNumbers());
			entity.setUserId(config.getUserid());
			
			if(config.getIsMt().equals("1")){
				//移动mt线程开启
				new RunZb003ThreadHttp("mt", entity).start();
			}
			if(config.getIsRpt().equals("1")){
				//移动rpt线程开启
				new RunZb003ThreadHttp("rpt", entity).start();
			}
			if(config.getIsMo().equals("1")){
				//移动mo线程开启
				new RunZb003ThreadHttp("mo", entity).start();
			}
			/*//分别启动子线程
			if(Constants.MOBILE.equals(config.getOperators())){
				//如果是移动，则启动移动的线程
				if(config.getIsMt().equals("1")){
					//移动mt线程开启
					new RunZb000ThreadHttp("mt", entity).start();
				}
				if(config.getIsRpt().equals("1")){
					//移动rpt线程开启
					new RunZb000ThreadHttp("rpt", entity).start();
				}
				if(config.getIsMo().equals("1")){
					//移动mo线程开启
					new RunZb000ThreadHttp("mo", entity).start();
				}
			}
			if(Constants.TELECOM.equals(config.getOperators())){
				//如果是电信，则启动电信的线程
				if(config.getIsMt().equals("1")){
					//移动mt线程开启
					new RunZb200ThreadHttp("mt", entity).start();
				}
				if(config.getIsRpt().equals("1")){
					//移动rpt线程开启
					new RunZb200ThreadHttp("rpt", entity).start();
				}
				if(config.getIsMo().equals("1")){
					//移动mo线程开启
					new RunZb200ThreadHttp("mo", entity).start();
				}
			}
			if(Constants.UNICOM.equals(config.getOperators())){
				//如果是联通，则启动联通的线程
				if(config.getIsMt().equals("1")){
					//移动mt线程开启
					new RunZb100ThreadHttp("mt", entity).start();
				}
				if(config.getIsRpt().equals("1")){
					//移动rpt线程开启
					new RunZb100ThreadHttp("rpt", entity).start();
				}
				if(config.getIsMo().equals("1")){
					//移动mo线程开启
					new RunZb100ThreadHttp("mo", entity).start();
				}
			}*/
		}
	}
}
